package com.example.shardingspheredemo.algorithm.standard;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.math.BigInteger;
import java.util.Collection;

/**
 * 数据库的精确查询分片
 */
public class MyPreciseDSShardingAlgorithm implements PreciseShardingAlgorithm {
	@Override
	public String doSharding(Collection collection, PreciseShardingValue preciseShardingValue) {
		String logicTableName = preciseShardingValue.getLogicTableName();
		BigInteger value = new BigInteger(String.valueOf((Long) preciseShardingValue.getValue()));
		String key = "m" + value.mod(new BigInteger("2")).add(new BigInteger("1"));
		if (collection.contains(key)) {
			return key;
		}
		throw new UnsupportedOperationException(key + "is not support");
	}
}
